﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;

namespace BAL
{
    public class JpegHelper
    {
        private const string IMAGE_EXTENSION = "\\.(jpg|jpeg|png)$";
        public static bool HasJpegExtension(string filename)
        {
            return Regex.IsMatch(filename, IMAGE_EXTENSION, RegexOptions.IgnoreCase);
        }

        public static bool HasJpegHeader(Stream s)
        {
            using (BinaryReader br = new BinaryReader(s))
            {
                UInt16 soi = br.ReadUInt16();  // Start of Image (SOI) marker (FFD8)
                UInt16 jfif = br.ReadUInt16(); // JFIF marker (FFE0)
                br.Close();
                return soi == 0xd8ff && jfif == 0xe0ff;
            }
        }
    }
}
